package Util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DbUtil {

    private static String jdbcName;
    private static String dburl;
    private static String dbUserName;
    private static String dbPassWord;

    //进行成员静态的初始化操作
    static {
        jdbcName = "com.mysql.jdbc.Driver";
        dburl = "jdbc:mysql://localhost:3306/db_web1";
        dbUserName = "root";
        dbPassWord = "root";
        try {
            Class.forName (jdbcName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace ();
        }
    }
    //获取连接
    public static Connection getConnection() throws SQLException {
        Connection connection = DriverManager.getConnection (dburl, dbUserName, dbPassWord);
        return connection;
    }
    //关闭连接
    public static void closeReasource(Connection connection, PreparedStatement preparedStatement) throws SQLException {
        if (null != connection) {
            connection.close ();
        }
        if (null != preparedStatement) {
            preparedStatement.close ();
        }
    }
}
